package com.tjsinfo.tjsoa.fileExpress.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cnblogs.zxub.upload.HttpFileUpload;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListAndFilePending;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressFileBo;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressGroupBo;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressUserSizeBo;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressFile;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressGroup;
import com.tjsinfo.tjsoa.fileExpress.vo.TjsUserEmail;
import com.tjsinfo.tjsoa.message.bo.ITjsMessageBo;
import com.tjsinfo.tjsoa.message.vo.TjsMessage;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class ZhuanFileAction extends Action {
	private IFileExpressUserSizeBo userEmailBo;
	private IFileExpressGroupBo expressGroupBo;
	private ISystemUserBo tjsUserBo;
	private IFileExpressFileBo fileExpressFileBo;
	private IAddressListAndFilePendingBo addressListAndFilePendingBo;
	/*private ITjsMessageBo messageBo;
	public void setMessageBo(ITjsMessageBo messageBo) {
		this.messageBo = messageBo;
	}*/

	public void setAddressListAndFilePendingBo(
			IAddressListAndFilePendingBo addressListAndFilePendingBo) {
		this.addressListAndFilePendingBo = addressListAndFilePendingBo;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String returnUrl = "ok";
		String returnString = "";

		String updateStr = "";
		ArrayList listFileName = new ArrayList();
		ArrayList fasongUser = new ArrayList();
		String zhuan_fileid = request.getParameter("zhuan_fileid");
		String zhuan_userid = request.getParameter("zhuan_userid");

		int filesize = 0;
		List<FileExpressFile> fileList = new ArrayList();
		for (int i = 0; i < zhuan_fileid.split(",").length; i++) {
			if (!"".equals(zhuan_fileid.split(",")[i])) {
				FileExpressFile file = fileExpressFileBo.findById(zhuan_fileid.split(",")[i]);
				fileList.add(file);
			}
		}
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		if (fileList != null) {
			Integer[] inte = new Integer[zhuan_userid.split(",").length];
			for (int j = 0; j < inte.length; j++) {
				if (zhuan_userid.split(",")[j] != null) {
					inte[j] = Integer.valueOf(zhuan_userid.split(",")[j]);
				}
			}
			List<TSystemUser> list = tjsUserBo.findInUserIds(inte);
			
			boolean bl = true;
			for (Iterator iterator = list.iterator(); iterator
					.hasNext();) {
				TSystemUser user1 = (TSystemUser) iterator.next();
				
				fasongUser.add(user1.getUserName());
				for (FileExpressFile files : fileList) {
					if (bl) {
						listFileName.add(files.getFileName());
					}
					FileExpressGroup expressGroup = new FileExpressGroup();
					expressGroup = expressGroupBo.findByMainGroup(
							user1.getUserId(),
							"我的接收文件夹");
					String id = UUID.randomUUID().toString().replace("-", "");
					fileExpressFileBo.insert(id,expressGroup.getId(), user.getUser_id(),files.getFileName(), sdf.format(new Date()), files.getExpressLevel().toString(), files.getFileSize().toString(), files.getFilePath(), files.getRemark());
				}
				bl = false;
			}
			for (int i = 0; i < zhuan_userid.split(",").length; i++) {
				if (!"".equals(zhuan_userid.split(",")[i])) {
					Integer userId = Integer.valueOf(zhuan_userid.split(",")[i]);
					AddressListAndFilePending message = new AddressListAndFilePending();
					message.setId(UUID.randomUUID().toString().replace("-", ""));
					message.setSendUserId(user.getUser_id());
					message.setSendUserName(user.getUsername());
					message.setCreateTime(new Date());
					message.setTjsUserId(userId);
					
					message.setTitle("发送新文件");
					message.setType("文件速递");
					String path1 = request.getContextPath();
					String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path1+"/fileExpress/frame.jsp";
					message.setUrl(basePath);
					
					addressListAndFilePendingBo.save(message);
					
//					TjsMessage message1 = new TjsMessage();
//					message1.setSendusername(user.getUsername());
//					message1.setRecusername(recusername);
//					message1.setValue(user.getUsername()+"向您发送文件，请登录TJSOA进行处理。");
//					tjsMessageBo.sendNewMessage(message1);
				}
			}
			
			String username = request.getParameter("zhuan_username");
			if (username!=null && !"".equals(username)) {
				String[] str = username.split(",");
				for (int i = 0; i < str.length; i++) {
					if(str[i]!=null && !"".equals(str[i])){
						TjsMessage message1 = new TjsMessage();
						message1.setSendusername(user.getUsername());
						message1.setRecusername(str[i]);
						message1.setValue(user.getUsername()+"向您发送文件，请登录TJSOA进行处理。");
						//TjsMessage.send(message1);
					}
				}
			}
		}
		request.setAttribute("returnString", returnString);
		request.setAttribute("listFileName", listFileName);
		request.setAttribute("fasongUser", fasongUser);
		return mapping.findForward("ok");
	}

	public IFileExpressGroupBo getExpressGroupBo() {
		return expressGroupBo;
	}

	public void setExpressGroupBo(IFileExpressGroupBo expressGroupBo) {
		this.expressGroupBo = expressGroupBo;
	}

	public long getFileSize(File f) throws Exception {
		long size = 0;
		File flist[] = f.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}

	public void setUserEmailBo(IFileExpressUserSizeBo userEmailBo) {
		this.userEmailBo = userEmailBo;
	}

	public void setTjsUserBo(ISystemUserBo tjsUserBo) {
		this.tjsUserBo = tjsUserBo;
	}

	public void setFileExpressFileBo(IFileExpressFileBo fileExpressFileBo) {
		this.fileExpressFileBo = fileExpressFileBo;
	}
}
